using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Media.Media3D;
using System.Windows;
using System.Windows.Media;
using System.Windows.Controls;
using System.Collections;

namespace _3DHouse.CustomControl
{
   /// <summary>
    /// 所有模型的父类,包含公共属性和方法
   /// </summary>
    public class Cube : ModelVisual3D
    {
        /// <summary>
        /// 变换属性的依赖项属性
        /// </summary>
        public static readonly new DependencyProperty TransformProperty = DependencyProperty.Register("Transform", typeof(Transform3D), typeof(Cube),
            new FrameworkPropertyMetadata(new PropertyChangedCallback(OnTransformChange)));
    
      /// <summary>
        /// 文字属性的依赖项属性
        /// </summary>     
        public static readonly DependencyProperty TextProperty = DependencyProperty.Register("Text", typeof(string), typeof(Cube),
          new FrameworkPropertyMetadata(new PropertyChangedCallback(OnTextChange)));
        /// <summary>  
        /// 获取或设置在3D模型上设置的文字
        /// </summary>
        public string Text
        {
            get
            {
                if (GetValue(TextProperty) == null)
                {
                    return null;
                }
                else
                {
                    return GetValue(TextProperty).ToString();
                }
            }
            set
            {
                SetValue(TextProperty, value);
            }
        }
        /// <summary>
        /// 文字更改触发的事件，修改模型的文字
        /// </summary>     
        static void OnTextChange(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            bool isChange = false;
            Cube cube = (Cube)sender;//获得当前对象
            if (cube.Children.Count > 0)//判断子对象个数是否大于0
            {
                foreach (Visual3D element in cube.Children)//遍历每个对象
                {
                    if (element is Viewport2DVisual3D)//判断是否是二维对象
                    {
                        Viewport2DVisual3D view = element as Viewport2DVisual3D;
                        //判断是否是文本
                        if (view.Visual is Label)
                        {
                            Label txt = view.Visual as Label;
                            txt.Content = cube.Text;
                            isChange = true;
                        }
                    }
                }
            }
             if(isChange==false)//如果没有子对象就是没有文本。
            {
                if (cube.Content != null)
                {
                    cube.GetText(cube.Text);
                }
            }
        }
        /// <summary>
        /// 设置模型的文字，创建一个三维中的二维对象Label
        /// </summary>
        /// <param name="text">文字内容</param>
        private void GetText(string text)
        {
            Viewport2DVisual3D viewport2D = new Viewport2DVisual3D();
            MeshGeometry3D mesh = new MeshGeometry3D();
            //设置文字的位置，添加点的集合
            //第一个点左下角。Bounds属性的X，Y，Z是模型后面左下角的点,Z加0.05是为了让文字在模型前面
            //Point3D p1 = new Point3D(Content.Bounds.X, Content.Bounds.Y, Content.Bounds.Z + Content.Bounds.Size.Z + 0.05);
            ////右下角
            //Point3D p2 = new Point3D(Content.Bounds.X + Content.Bounds.SizeX, Content.Bounds.Y, Content.Bounds.Z + Content.Bounds.Size.Z + 0.05);
            ////右上角
            //Point3D p3 = new Point3D(Content.Bounds.X + Content.Bounds.SizeX, Content.Bounds.Y + 0.5, Content.Bounds.Z + Content.Bounds.Size.Z + 0.05);
            ////左上角 
            //Point3D p4 = new Point3D(Content.Bounds.X, Content.Bounds.Y + 0.5, Content.Bounds.Z + Content.Bounds.Size.Z + 0.05);
            Point3D p1 = new Point3D(Content.Bounds.X + Content.Bounds.SizeX, Content.Bounds.SizeY + Content.Bounds.Y, Content.Bounds.Z + 1);
            //右下角
            Point3D p2 = new Point3D(Content.Bounds.X + Content.Bounds.SizeX + 1, Content.Bounds.SizeY + Content.Bounds.Y, Content.Bounds.Z + 1);
            //右上角
            Point3D p3 = new Point3D(Content.Bounds.X + Content.Bounds.SizeX + 1, Content.Bounds.SizeY + Content.Bounds.Y, Content.Bounds.Z);
            //左上角 
            Point3D p4 = new Point3D(Content.Bounds.X + Content.Bounds.SizeX, Content.Bounds.SizeY + Content.Bounds.Y, Content.Bounds.Z);
            Point3DCollection pc = new Point3DCollection();
            pc.Add(p1);
            pc.Add(p2);
            pc.Add(p3);
            pc.Add(p4);
            mesh.Positions = pc;
            //点的索引集合
            Int32Collection myTriangleIndicesCollection = new Int32Collection();
            myTriangleIndicesCollection.Add(0);
            myTriangleIndicesCollection.Add(1);
            myTriangleIndicesCollection.Add(2);
            myTriangleIndicesCollection.Add(3);
            myTriangleIndicesCollection.Add(0);
            myTriangleIndicesCollection.Add(2);
            mesh.TriangleIndices = myTriangleIndicesCollection;
            //  纹理集合
            PointCollection pionTexture = new PointCollection();
            Point pt1 = new Point(0, 1);
            Point pt2 = new Point(1, 1);
            Point pt3 = new Point(1, 0);
            Point pt4 = new Point(0, 0);
            pionTexture.Add(pt1);
            pionTexture.Add(pt2);
            pionTexture.Add(pt3);
            pionTexture.Add(pt4);
            mesh.TextureCoordinates = pionTexture;
            //添加图形
            viewport2D.Geometry = mesh;
            DiffuseMaterial diff = new DiffuseMaterial();
            Viewport2DVisual3D.SetIsVisualHostMaterial(diff, true);
            viewport2D.Material = diff;
            //创建二维对象     
            Label word = new Label();
            //word.Background = new SolidColorBrush(Colors.Red);
            word.Content = text;
            word.FontSize = 9;
            word.HorizontalContentAlignment = HorizontalAlignment.Left;
            viewport2D.Visual = word;//放置在三维对象上的二维可视化对象
            Children.Add(viewport2D);//添加到模型中
        }
     string name;
        /// <summary>
        /// 获取或设置在3D模型上设置的名称
        /// </summary>
        public string Name
        {
            get
            {
                return name;
            }
            set
            {
                name= value;
            }
        }
        string type;
        /// <summary>
        /// 模型类型
        /// </summary>
        public string Type
        {
            get { return type; }
            set { type = value; }
        }
        bool resize;
        /// <summary>
        /// 修改尺寸
        /// </summary>
        public bool Resize
        {
            get { return resize; }
            set { resize = value; }
        }
        /// <summary>
        ///  获取或设置在3D模型上设置的转换
        /// </summary>       
        public new Transform3D Transform
        {
            get
            {
                return GetValue(TransformProperty) as Transform3D;
            }
            set
            {
                SetValue(TransformProperty, value);
            }
        }
        
        /// <summary>
        /// 变换属性更改触发的事件，更改模型的坐标
        /// </summary>
        static void OnTransformChange(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            
            Cube cube = sender as Cube;
           
                if (cube.Content != null)
                {
                    cube.SetTransform(cube);
                    //还原变换值以便于触发change事件
                    cube.ClearTransform(cube.Transform);
                }            
            //判断children
            //children里面是Modelvisual还是Viewport2DVisual3D
        }
        /// <summary>
        /// 设置模型坐标变换
        /// </summary>
        /// <param name="cube">模型对象</param>
        protected void SetTransform(Cube cube)
        {
            //判断是否是Model3DGroup
            if (cube != null && cube.Content is Model3DGroup)
            {
                //获得Model3DGroup
                Model3DGroup group = cube.Content as Model3DGroup;
                #region 获得坐标分量集合
                ArrayList arrX = new ArrayList();
                ArrayList arrY = new ArrayList();
                ArrayList arrZ = new ArrayList();
                //遍历所以model,进行排序获得边界值
                int count = 0;//数组的索引
                for (int m = 0; m < group.Children.Count; m++)
                {
                    //改变单个Model的坐标
                    GeometryModel3D geome = group.Children[m] as GeometryModel3D;
                    MeshGeometry3D mesh = geome.Geometry as MeshGeometry3D;
                    //把各轴坐标做成数组
                    for (int n = 0; n < mesh.Positions.Count; n++)
                    {
                        arrX.Add(mesh.Positions[n].X);
                        arrY.Add(mesh.Positions[n].Y);
                        arrZ.Add(mesh.Positions[n].Z);
                        count++;
                    }
                }
                //排序
                arrX.Sort();
                arrY.Sort();
                arrZ.Sort();
                #endregion
                //设置每个坐标的变换
                for (int m = 0; m < group.Children.Count; m++)
                {
                    //改变单个Model的坐标
                    GeometryModel3D geome = group.Children[m] as GeometryModel3D;
                    MeshGeometry3D mesh = geome.Geometry as MeshGeometry3D;
                    cube.JudgeTransform(mesh, cube.Transform, arrX, arrY, arrZ);
                    //cube.SetTransform(mesh, cube);
                    //Console.WriteLine(cube.Content.Bounds);
                    // Console.WriteLine(".X:" + arrX[0] + " z:" + arrZ[0] + " maxX" + arrX[arrX.Count - 1]);
                }
                //文字跟随模型变换
                SetViewport2DVisual3D(cube, arrX, arrY, arrZ);
            }
            else if (cube.Content != null && cube.Content is GeometryModel3D)//单个Mode
            {
                GeometryModel3D geome = cube.Content as GeometryModel3D;
                MeshGeometry3D mesh = geome.Geometry as MeshGeometry3D;
                ArrayList arrX = new ArrayList();
                ArrayList arrY = new ArrayList();
                ArrayList arrZ = new ArrayList();
                //把各轴坐标做成数组
                for (int n = 0; n < mesh.Positions.Count; n++)
                {
                    arrX.Add(mesh.Positions[n].X);
                    arrY.Add(mesh.Positions[n].Y);
                    arrZ.Add(mesh.Positions[n].Z);
                }
                cube.JudgeTransform(mesh, cube.Transform, arrX, arrY, arrZ);
                // cube.SetTransform(mesh, cube);
                SetViewport2DVisual3D(cube, arrX, arrY, arrZ);
            }
        }
        /// <summary>
        /// 判断变换是单个还是多个
        /// </summary>
        /// <param name="mesh">要更改的3D基本图形</param>
        /// <param name="transform">模型的变换</param>
        /// <param name="arrX">X轴分量集合</param>
        /// <param name="arrY">Y轴分量集合</param>
        /// <param name="arrZ">Z轴分量集合</param>
        void JudgeTransform(MeshGeometry3D mesh, Transform3D transform, ArrayList arrX, ArrayList arrY, ArrayList arrZ)
        {
            //判断是否是变化组合
            if (transform is Transform3DGroup)
            {//判断三种变换
                Transform3DGroup tfg = transform as Transform3DGroup;
                foreach (Transform3D trans in tfg.Children)
                {
                    TransformInfo(mesh, trans, arrX, arrY, arrZ);
                }
            }
            else
            {
                TransformInfo(mesh, transform, arrX, arrY, arrZ);
            }
        }
        /* void SetTransform(MeshGeometry3D mesh,Cube cube)
          {
              //判断是否是变化组合
              if (cube.Transform is Transform3DGroup)
              {//判断三种变换
                  Transform3DGroup tfg = cube.Transform as Transform3DGroup;
                  foreach (Transform3D trans in tfg.Children)
                  {
                     // TransformInfo(mesh, trans, arrX, arrY, arrZ);
                      TransformInfo(mesh, trans,cube.Content.Bounds);
                  }
              }
              else
              {                
                 // TransformInfo(mesh, transform, arrX, arrY, arrZ);
                  TransformInfo(mesh, cube.Transform, cube.Content.Bounds);
              }
          }
          /// <summary>
          /// 更改变换
          /// </summary>
          /// <param name="mesh">要更改的3D基本图形</param>
          /// <param name="transform">3D变换</param>
         void TransformInfo(MeshGeometry3D mesh, Transform3D transform,Rect3D bounds)
         {
             //获得模型的边界值
             double maxX = bounds.SizeX+bounds.X;
             double minX = bounds.X;
             double maxZ = bounds.Z + bounds.SizeZ;
             double minZ = bounds.Z;
             double minY = bounds.Y;          
             if (transform is TranslateTransform3D)
             {
                 TranslateTransform3D trans = transform as TranslateTransform3D;
                 Point3DCollection point3ds = TransformCoordinate(mesh, trans);
                 mesh.Positions = point3ds;
             }
             else if (transform is RotateTransform3D)
             {
                 Point3DCollection point3ds = new Point3DCollection();
                 RotateTransform3D trans = transform as RotateTransform3D;
                 AxisAngleRotation3D axi = trans.Rotation as AxisAngleRotation3D;
                 double angle = axi.Angle * Math.PI / 180;//角度             
                 double moveX = (maxX - minX) / 2 + minX;
                 double moveZ = (maxZ - minZ) / 2 + minZ;
                 for (int n = 0; n < mesh.Positions.Count; n++)//遍历每个点
                 {
                     Point3D point = mesh.Positions[n];
                     Point3D pontNew = new Point3D();
                     point.X -= moveX;//平移回原点。以y轴旋转
                     point.Z -= moveZ;
                     pontNew.X = point.X * Math.Cos(angle) + point.Z * Math.Sin(angle) + moveX;
                     pontNew.Y = point.Y;
                     pontNew.Z = point.Z * Math.Cos(angle) - point.X * Math.Sin(angle) + moveZ;
                     point3ds.Add(pontNew);
                 }
                 mesh.Positions = point3ds;
             }
             else if (transform is ScaleTransform3D)
             {
                 Point3DCollection point3ds = new Point3DCollection();
                 ScaleTransform3D scale = transform as ScaleTransform3D;
                 //要移动的距离
                 TranslateTransform3D translate = new TranslateTransform3D();

                 //如果是x轴那么x最小的是要移动的距离，y轴是y值最小的
                 //保存x坐标的值的数组              
                 //x轴移动距离
                 if (scale.ScaleX != 1 && translate.OffsetX == 0)
                 {
                     translate.OffsetX = minX;// mesh.Positions[0].X;
                 }
                 //y轴移动距离
                 if (scale.ScaleY != 1 && translate.OffsetY == 0)
                 {

                     translate.OffsetY = minY;// mesh.Positions[0].X;
                 }
                 if (scale.ScaleZ != 1 && translate.OffsetZ == 0)
                 {
                     translate.OffsetZ = minZ;
                 }
                 for (int n = 0; n < mesh.Positions.Count; n++)//遍历每个点
                 {
                     //首先要平移到原点，然后拉伸，最后在平移回来
                     Point3D point = mesh.Positions[n];
                     point.X = (point.X - translate.OffsetX) * scale.ScaleX + translate.OffsetX;
                     point.Y = (point.Y - translate.OffsetY) * scale.ScaleY + translate.OffsetY;
                     point.Z = (point.Z - translate.OffsetZ) * scale.ScaleZ + translate.OffsetZ;
                     point3ds.Add(point);
                 }
                 mesh.Positions = point3ds;
             }
         }*/
        /// <summary>
        /// 三种坐标变换
        /// </summary>
        /// <param name="mesh">要更改的3D基本图形</param>
        /// <param name="transform">模型的变换</param>
        /// <param name="arrX">X轴分量集合</param>
        /// <param name="arrY">Y轴分量集合</param>
        /// <param name="arrZ">Z轴分量集合</param>
        void TransformInfo(MeshGeometry3D mesh, Transform3D transform, ArrayList arrX, ArrayList arrY, ArrayList arrZ)
        { //获得模型的边界值
            double maxX = (double)arrX[arrX.Count - 1];//最大X值
            double minX = (double)arrX[0];//最小X值
            double maxZ = (double)arrZ[arrZ.Count - 1];//最大Z值
            double minZ = (double)arrZ[0];//最小Z值
            double minY = (double)arrY[0];         //最小Y值 `
            //平移变换
            if (transform is TranslateTransform3D)
            {
                TranslateTransform3D trans = transform as TranslateTransform3D;
                Point3DCollection point3ds = TransformCoordinate(mesh, trans);
                mesh.Positions = point3ds;  //更改3D基本图形的坐标
            }
            //旋转变换
            else if (transform is RotateTransform3D)
            {
                Point3DCollection point3ds = new Point3DCollection();
                RotateTransform3D trans = transform as RotateTransform3D;
                AxisAngleRotation3D axi = trans.Rotation as AxisAngleRotation3D;
                double angle = axi.Angle * Math.PI / 180;//角度             
                double moveX = (maxX - minX) / 2 + minX;//要移动的X轴距离
                double moveZ = (maxZ - minZ) / 2 + minZ;//要移动的Z轴距离
                for (int n = 0; n < mesh.Positions.Count; n++)//遍历每个点
                {
                    Point3D point = mesh.Positions[n];
                    Point3D pontNew = new Point3D();
                    point.X -= moveX;//平移回原点。以y轴旋转
                    point.Z -= moveZ;
                    //计算旋转后的坐标点分量
                    pontNew.X = point.X * Math.Cos(angle) + point.Z * Math.Sin(angle) + moveX;
                    pontNew.Y = point.Y;
                    pontNew.Z = point.Z * Math.Cos(angle) - point.X * Math.Sin(angle) + moveZ;
                    point3ds.Add(pontNew);
                }
                mesh.Positions = point3ds;//更改3D基本图形的坐标
            }
            //拉伸变换
            else if (transform is ScaleTransform3D&&this.Resize)
            {
                Point3DCollection point3ds = new Point3DCollection();
                ScaleTransform3D scale = transform as ScaleTransform3D;
                //要移动的距离
                // TranslateTransform3D translate = new TranslateTransform3D();

                //如果是x轴那么x最小的是要移动的距离，y轴是y值最小的
                //保存x坐标的值的数组              
                //x轴移动距离
                //if (scale.ScaleX != 1 && translate.OffsetX == 0)
                //{
                //    translate.OffsetX = minX;// mesh.Positions[0].X;
                //}
                ////y轴移动距离
                //if (scale.ScaleY != 1 && translate.OffsetY == 0)
                //{                    
                //    translate.OffsetY = minY;// mesh.Positions[0].X;
                //}
                //if (scale.ScaleZ != 1 && translate.OffsetZ == 0)
                //{
                //    translate.OffsetZ = minZ;
                //}
                for (int n = 0; n < mesh.Positions.Count; n++)//遍历每个点
                {
                    //首先要平移到原点，然后拉伸，最后在平移回来
                    Point3D point = mesh.Positions[n];
                    point.X = (point.X - minX) * scale.ScaleX + minX;
                    point.Y = (point.Y - minY) * scale.ScaleY + minY;
                    point.Z = (point.Z - minZ) * scale.ScaleZ + minZ;
                    point3ds.Add(point);                  
                }
                num += scale.ScaleX;
                mesh.Positions = point3ds;
            }
        }
        double num;
        /// <summary>
        /// 平移变换坐标
        /// </summary>
        /// <param name="mesh">MeshGeometry3D对象</param>
        /// <param name="trans">平移变换，保存平移的值</param>
        /// <returns></returns>
        private Point3DCollection TransformCoordinate(MeshGeometry3D mesh, TranslateTransform3D trans)
        {
            Point3DCollection point3ds = new Point3DCollection();
            for (int n = 0; n < mesh.Positions.Count; n++)//遍历每个点
            {
                Point3D point = mesh.Positions[n];
                point.X += trans.OffsetX;
                point.Y += trans.OffsetY;
                point.Z += trans.OffsetZ;
                point3ds.Add(point);
            }
            return point3ds;
        }

        #region 还原变换值以便于触发change事件
        void ClearTransform(Transform3D transform)
        {
            //判断是否是变化组合
            if (transform is Transform3DGroup)
            {//判断三种变换
                Transform3DGroup tfg = transform as Transform3DGroup;
                foreach (Transform3D trans in tfg.Children)
                {
                    ClearTransformInfo(trans);
                }
            }
            else
            {
                ClearTransformInfo(transform);
            }
        }
        void ClearTransformInfo(Transform3D transform)
        {
            if (transform is TranslateTransform3D)
            {
                TranslateTransform3D trans = transform as TranslateTransform3D;
                trans.OffsetX = 0;
                trans.OffsetY = 0;
                trans.OffsetZ = 0;
            }
            else if (transform is RotateTransform3D)
            {
                RotateTransform3D trans = transform as RotateTransform3D;
                AxisAngleRotation3D axi = trans.Rotation as AxisAngleRotation3D;
                axi.Angle = 0;//角度

            }
            else if (transform is ScaleTransform3D)
            {
                ScaleTransform3D trans = transform as ScaleTransform3D;
                trans.ScaleX = 1;
                trans.ScaleY = 1;
                trans.ScaleZ = 1;
            }
        }
        #endregion
        #region 更改资源中模型坐标
        static double scaleX = 1;//保存变换值
        static double scaleY = 1;
        static double scaleZ = 1;
        static double offsetX = 0;
        static double offsetY = 0;
        static double offsetZ = 0;
        /// <summary>
        /// 更改资源中的变换为坐标变换
        /// </summary>
        /// <param name="geome">3D模型的集合</param>
        /// <returns></returns>
        internal static Model3DGroup TransformResources(Model3DGroup model3DGroup)
        {
            Model3DGroup group = new Model3DGroup();
            foreach (GeometryModel3D geome in model3DGroup.Children)
            {
                group.Children.Add(TransformResources(geome));
            }
            return group;
        }
        /// <summary>
        /// 更改资源中的变换为坐标变换
        /// </summary>
        /// <param name="geome">单个3D模型</param>
        /// <returns></returns>
        internal static GeometryModel3D TransformResources(GeometryModel3D geome)
        {
            scaleX = 1;
            scaleY = 1;
            scaleZ = 1;
            offsetX = 0;
            offsetY = 0;
            offsetZ = 0;
            //新模型
            GeometryModel3D ge = new GeometryModel3D();
            //判断是否是变化组合
            if (geome.Transform is Transform3DGroup)
            {//判断三种变换
                Transform3DGroup tfg = geome.Transform as Transform3DGroup;
                foreach (Transform3D trans in tfg.Children)
                {
                    TransformResourcesInfo(trans);
                }
            }
            else
            {
                TransformResourcesInfo(geome.Transform);
            }

            MeshGeometry3D mesh = geome.Geometry as MeshGeometry3D;
            ge.Material = geome.Material;
            MeshGeometry3D me = mesh.Clone();
            ge.Geometry = me;
            Point3DCollection point3ds = new Point3DCollection();

            for (int n = 0; n < mesh.Positions.Count; n++)//遍历每个点
            {
                Point3D point = mesh.Positions[n];
                point.X = scaleX * point.X + offsetX;
                point.Y = scaleY * point.Y + offsetY;
                point.Z = scaleZ * point.Z + offsetZ;
                point3ds.Add(point);
            }
            me.Positions = point3ds;

            return ge;
        }
        //protected GeometryModel3D TransformResources(Viewport2DVisual3D geome)
        //{
        //    scaleX = 1;
        //    scaleY = 1;
        //    scaleZ = 1;
        //    offsetX = 0;
        //    offsetY = 0;
        //    offsetZ = 0;
        //    //新模型
        //    GeometryModel3D ge = new GeometryModel3D();
        //    //判断是否是变化组合
        //    if (geome.Transform is Transform3DGroup)
        //    {//判断三种变换
        //        Transform3DGroup tfg = geome.Transform as Transform3DGroup;
        //        foreach (Transform3D trans in tfg.Children)
        //        {
        //            TransformResourcesInfo(trans);
        //        }
        //    }
        //    else
        //    {
        //        TransformResourcesInfo(geome.Transform);
        //    }

        //    MeshGeometry3D mesh = geome.Geometry as MeshGeometry3D;
        //    ge.Material = geome.Material;
        //    MeshGeometry3D me = mesh.Clone();
        //    ge.Geometry = me;
        //    Point3DCollection point3ds = new Point3DCollection();

        //    for (int n = 0; n < mesh.Positions.Count; n++)//遍历每个点
        //    {
        //        Point3D point = mesh.Positions[n];
        //        point.X = scaleX * point.X + offsetX;
        //        point.Y = scaleY * point.Y + offsetY;
        //        point.Z = scaleZ * point.Z + offsetZ;
        //        point3ds.Add(point);
        //    }
        //    me.Positions = point3ds;

        //    return ge;
        //} 

        /// <summary>
        /// 获得变换值
        /// </summary>
        /// <param name="transform"></param>
        internal static void TransformResourcesInfo(Transform3D transform)
        {

            if (transform is ScaleTransform3D)
            {
                // Point3DCollection point3ds = new Point3DCollection();
                ScaleTransform3D scale = transform as ScaleTransform3D;
                scaleX = scale.ScaleX;
                scaleY = scale.ScaleY;
                scaleZ = scale.ScaleZ;
                //for (int n = 0; n < mesh.Positions.Count; n++)//遍历每个点
                //{
                //    Point3D point = mesh.Positions[n];
                //    point.X = scale.ScaleX * point.X;
                //    point.Y = scale.ScaleY*point.Y;
                //    point.Z= scale.ScaleZ*point.Z;
                //    point3ds.Add(point);
                //}
                //me.Positions = point3ds;
            }
            else if (transform is RotateTransform3D)
            {
                // Point3DCollection point3ds = new Point3DCollection();
                RotateTransform3D trans = transform as RotateTransform3D;
                AxisAngleRotation3D axi = trans.Rotation as AxisAngleRotation3D;
                double angle = axi.Angle;//角度
                //for (int n = 0; n < mesh.Positions.Count; n++)//遍历每个点
                //{
                //    Point3D point = mesh.Positions[n];
                //    point.X = point.X * Math.Cos(angle) + point.Z * Math.Sin(angle);
                //    point.Y = point.Y;
                //    point.Z += point.Z * Math.Cos(angle) - point.X * Math.Sin(angle);
                //    point3ds.Add(point);
                //}
                //me.Positions = point3ds;
            }
            else if (transform is TranslateTransform3D)
            {
                TranslateTransform3D trans = transform as TranslateTransform3D;
                offsetX = trans.OffsetX;
                offsetY = trans.OffsetY;
                offsetZ = trans.OffsetZ;
            }


        }
        #endregion
        /// <summary>
        /// 设置2D图形的变换
        /// </summary>
        /// <param name="model">依附的3D图形</param>    
        /// <param name="arrX">X轴分量集合</param>
        /// <param name="arrY">Y轴分量集合</param>
        /// <param name="arrZ">Z轴分量集合</param>
        void SetViewport2DVisual3D(Cube model, ArrayList arrX, ArrayList arrY, ArrayList arrZ)
        {
            //Children
            //循环每个对象ModelVisual3D，Viewport2DVisual3D都是Visual3D对象
            foreach (Visual3D visual in model.Children)
            {
                //判断是ModelVisual3D
                // formodel(visual as ModelVisual3D);
                //Viewport2DVisual3D
                //遍历文字的方法
                if (visual is Viewport2DVisual3D)
                {
                    Viewport2DVisual3D visual2d = visual as Viewport2DVisual3D;
                    MeshGeometry3D ge = visual2d.Geometry as MeshGeometry3D;
                    model.JudgeTransform(ge, model.Transform, arrX, arrY, arrZ);
                    // model.SetTransform(ge, model);
                }
                //else if (visual is Cube)
                //{
                //    Cube cube = visual as Cube;
                //    SetTransform(cube);
                //}
            }
        }
     
    }
}
